#include #include using namespace std; const double PI = 3.141; const int ROWS = 4; const int COLUMNS = 4; //function aka method, routine, sub-routine, sub //documentation //modularization aka componentization aka hiding the mess //reuse void getMatrixFromUser(int matrix[][COLUMNS]) { for(int row = 0; row < ROWS; row++) { cout << " Row " << row + 1 << "? "; for(int column = 0; column < COLUMNS; column++) { cin >> matrix[row][column]; } } } void displayMatrix(int matrix[][COLUMNS]) { for(int row = 0; row < ROWS; row++) { cout << " Row " << row + 1 << ": "; for(int column = 0; column < COLUMNS; column++) { cout << matrix[row][column] << " "; } cout << endl; } } void matrixAdd(int matrix1[][COLUMNS], int matrix2[][COLUMNS], int answer[][COLUMNS]) { for(int row = 0; row < ROWS; row++) { for(int column = 0; column < COLUMNS; column++) { answer[row][column] = matrix1[row][column] + matrix2[row][column]; } } } int userSelectedIndex(int matricesUsed) { int result = 0; do { cin >> result; } while(result <1 || result > matricesUsed); result--; return result; } void main() { //literal or constant whole numbers int matrices[25][ROWS][COLUMNS]; int matricesUsed = 0; int initialMatrices; do { cout << "How many initial matrices? "; cin >> initialMatrices; } while(initialMatrices < 1 || initialMatrices > 25); while(matricesUsed < initialMatrices) { getMatrixFromUser(matrices[matricesUsed]); matricesUsed++; } char menuOption; cin >> menuOption; int firstMatrix; int secondMatrix; switch(menuOption) { case '+': cout << "First matrix for +?" ; firstMatrix = userSelectedIndex(matricesUsed); cout << "Second matrix for +?" ; secondMatrix = userSelectedIndex(matricesUsed); matrixAdd(matrices[firstMatrix], matrices[secondMatrix], matrices[matricesUsed]); matricesUsed++; break; case '-': cout << "First matrix for -?" ; firstMatrix = userSelectedIndex(matricesUsed); cout << "Second matrix for -?" ; secondMatrix = userSelectedIndex(matricesUsed); matrixSubtract(matrices[firstMatrix], matrices[secondMatrix], matrices[matricesUsed]); matricesUsed++; break; } displayMatrix(matrices[2]); }